﻿using System;
using System.Collections.Generic;
using System.Text;
using VoteServices;
using VoteServices.VoteClass;
using VoteServices.VoteModel;

namespace Vote
{
    /// <summary>
    /// 界面服务
    /// </summary>
    public static class InterfaceService
    {
        static ProblemService problemService = new ProblemService();
        static ProblemModel problemModel = new ProblemModel();
        static RecordService recordService = new RecordService();
        static UserService userService = new UserService();
        static List<Problem> problems = problemService.GetProblems();
        static Models models = new Models();
        static Voting voting = new Voting();
       /// <summary>
       /// 登录
       /// </summary>
       /// <returns></returns>
        public static User _Login()
        {
            string Account;
            string Password;
            User user = null;
            while (user == null)
            {
                Console.WriteLine("请输入帐号:");
                Account = Console.ReadLine();
                Console.WriteLine("请输入密码");
                Password = Console.ReadLine();
                user = userService.GetUser(Account, Password);
                if (user == null)
                {
                    Console.WriteLine("查无此人，请重新输入");
                }
            }
            return user;
        }
        /// <summary>
        /// 功能选择界面
        /// </summary>
        /// <param name="userModel"></param>
        public static void _FunctionSelect(UserModel userModel)
        {
            Console.WriteLine("你好" + userModel.Name);
            Console.WriteLine("请选择操作选项");
            Console.WriteLine("1:投票");
            Console.WriteLine("2:查看投票统计");
            Console.WriteLine("3:退出");
        }
        /// <summary>
        /// 投票
        /// </summary>
        /// <param name="user"></param>
        public static void Vote(User user)
        {
            Models models = new Models();
            Voting voting = new Voting();
            RecordService recordService = new RecordService();
            List<Problem> problems = problemService.GetProblems();
            for (int PromblemCount = 0; PromblemCount < problems.Count; PromblemCount++)
            {
                if (problemService.PastDue(problems[PromblemCount]))
                {
                    problemModel = models.problemModel(problems[PromblemCount]);
                    Console.WriteLine(PromblemCount+":"+problemModel.Detail + "\n");

                }
            }
            int Select = 0;
            while (Select!=-1)
            {
                Console.WriteLine("请选择要投票的问题,输入-1结束投票");
                string _Selcet = Console.ReadLine();
                while (int.TryParse(_Selcet, out Select) == false)
                {
                    Console.WriteLine("没有这个选项，请重新输入");
                    _Selcet = Console.ReadLine();
                }
                Select = int.Parse(_Selcet);
                if (Select == -1)
                    break;
                while (Select >= problems.Count || Select < 0)
                {
                    Console.WriteLine("没有这个选项，请重新输入");
                    Select = int.Parse(Console.ReadLine());
                }
                
                if (Select == -1)
                    break;

                problemModel = models.problemModel(problems[Select]);
                Console.WriteLine(problemModel.Detail);
                List<Option> options = problemService.GetOptions(problemModel.Id);
                for (int OptionCount = 0; OptionCount < options.Count; OptionCount++)
                {
                    Console.WriteLine(OptionCount + ":" + options[OptionCount].Detail);
                }
                Console.WriteLine("请输入你选择的选项");
                string _QuestinSelcet = Console.ReadLine();
                int QuestionSelect = -1;
                while (int.TryParse(_QuestinSelcet, out QuestionSelect) == false)
                {
                    Console.WriteLine("没有这个选项，请重新输入");
                    _QuestinSelcet = Console.ReadLine();
                }
                QuestionSelect = int.Parse(_QuestinSelcet);
                while (QuestionSelect >= options.Count || QuestionSelect < 0)
                {
                    Console.WriteLine("没有这个选项，请重新输入1");
                    QuestionSelect = int.Parse(Console.ReadLine());

                }
                
                if (recordService.examine(problems[Select].Id, user))
                            Console.WriteLine("您已经给这个问题投过票了");
                        else
                           voting.Vote(options[QuestionSelect].Id, user);

            }

            //for (int PromblemCount = 0; PromblemCount < problems.Count; PromblemCount++)
            //{
            //    if (problemService.PastDue(problems[PromblemCount]))
            //    {
            //        problemModel = models.problemModel(problems[PromblemCount]);
            //        Console.WriteLine(problemModel.Detail);
            //        List<Option> options = problemService.GetOptions(problemModel.Id);
            //        for (int OptionCount = 0; OptionCount < options.Count; OptionCount++)
            //        {
            //            Console.WriteLine(OptionCount + ":" + options[OptionCount].Detail);
            //        }
            //        Console.WriteLine("请输入你选择的选项");
            //        string _QuestinSelcet = Console.ReadLine();
            //        int QuestionSelect = -1;
            //        while(int.TryParse(_QuestinSelcet, out QuestionSelect) == false)
            //        {
            //            Console.WriteLine("没有这个选项，请重新输入");
            //            _QuestinSelcet = Console.ReadLine();
            //            QuestionSelect = int.Parse(_QuestinSelcet);
            //            while (QuestionSelect >= options.Count || QuestionSelect < 0)
            //            {
            //                Console.WriteLine("没有这个选项，请重新输入");
            //                QuestionSelect = int.Parse(Console.ReadLine());

            //            }  
            //        }

            //        if (recordService.examine(problems[PromblemCount].Id, user))
            //            Console.WriteLine("您已经给这个问题投过票了");
            //        else
            //            voting.Vote(options[QuestionSelect].Id, user);
            //    }
            //}
        }
        /// <summary>
        /// 查看投票结果
        /// </summary>
        public static void ViewResults()
        {
            ProblemService problemService=new ProblemService();
            ProblemModel problemModel = new ProblemModel();
            Models models = new Models();
            List<Problem> problems = problemService.GetProblems();
            for (int PromblemCount = 0; PromblemCount < problems.Count; PromblemCount++)
            {
                if (problemService.PastDue(problems[PromblemCount]))
                {
                    problemModel = models.problemModel(problems[PromblemCount]);
                    Console.WriteLine(problemModel.Detail);
                    List<Option> options = problemService.GetOptions(problemModel.Id);
                    for (int OptionCount = 0; OptionCount < options.Count; OptionCount++)
                    {
                        Console.WriteLine(OptionCount + ":" + options[OptionCount].Detail + " " + options[OptionCount].Votes + "票");
                    }
                }

            }
        }


    }
}
